﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class PageControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    PagedDataSource pds = new PagedDataSource();
    //待分页的数据源 
    public IEnumerable DataSource;
    //每页显示数据条数 
    public int pageSize;
    //待分页显示的DataList对象 
    public DataList datalist;
    //当前页码 
    public int CurrentPage
    {
        get { return (Int32)ViewState["CurrentPage"]; }
        set { ViewState["CurrentPage"] = value; }
    }
    //总页数
    public int Count
    {
        get { return (Int32)ViewState["Count"]; }
        set { ViewState["Count"] = value; }
    }
    //第一页
    protected void ImageBtfirst_Click(object sender, ImageClickEventArgs e)
    {
        if (CurrentPage == 1)
        {
             Jscript.ShowLocation ("当前已经是第一页！");
        }
        else
        {
            CurrentPage = 1;
            PageDataBind();
        }

    }
    //上一页
    protected void ImageBtprev_Click(object sender, ImageClickEventArgs e)
    {
        if (CurrentPage <= 1)
        {
            CurrentPage = 1;
        }
        else
        {
            CurrentPage = CurrentPage - 1;
        }
        PageDataBind();
    }
    //下一页
    protected void ImageBtnext_Click(object sender, ImageClickEventArgs e)
    {
        if (CurrentPage >= Count)
        {
            CurrentPage = Count;
        }
        else
        {
            CurrentPage = CurrentPage + 1;
        }
        PageDataBind();
    }
    //最后一页
    protected void ImageBttail_Click(object sender, ImageClickEventArgs e)
    {
        if (CurrentPage == Count)
        {  
            Jscript.ShowLocation ("当前已经是最后一页！");
        }
        else
        {
            CurrentPage = Count;
            PageDataBind();
        }
    }
    //跳转到某页
    protected void ImageBtgo_Click(object sender, ImageClickEventArgs e)
    {
        if (this.txtGoPage.Text != null)
        {
            try
            {
                int i = int.Parse(this.txtGoPage.Text);

                if (i > Count || i < 0)
                {
                     Jscript.ShowLocation ("请输入正确的范围！");
                }
                else
                {
                    CurrentPage = int.Parse(this.txtGoPage.Text);
                    PageDataBind();
                }
            }
            catch
            {
                Jscript.ShowLocation("请输入正确的页码！");
            }
        }


        else
        {
             Jscript.ShowLocation ("请输入你要跳转的页！");
        }
    }

    //数据绑定 
    public void PageDataBind()
    {
        if (!IsPostBack)
        {
            CurrentPage = 1;
            Count = 1;
        }
        pds.DataSource = DataSource;
        pds.AllowPaging = true;
        pds.PageSize = pageSize;
        pds.CurrentPageIndex = CurrentPage - 1;
        Count = pds.PageCount;
        this.lblCurrentPage.Text = Convert.ToString(CurrentPage);
        this.lblTotalPage.Text = Convert.ToString(Count);
        this.lblPageSize.Text = pageSize.ToString();
        this.lblTotalCount.Text = pds.DataSourceCount.ToString();
        datalist.DataSource = pds;
        datalist.DataBind();
    }
}
